package com.live.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




import com.google.gson.Gson;
import com.live.model.Admin;
import com.live.service.AdminService;
import com.live.util.DesTool;
import com.live.util.StringUtil;


@WebServlet("/adminGetAccount")
public class AdminController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminService adminService;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter("code").trim();
		String password = request.getParameter("password").trim();
		String account = request.getParameter("account").trim();
		HttpSession session = request.getSession(true);
		String sessionCode = (String)session.getAttribute(CodeController.IMG_CODE);//获取session中的验证码
		String str = "";
		String errInfo = "";
		if (sessionCode.equalsIgnoreCase(code)) {
				adminService = new AdminService();
				Admin admin  = adminService.getAdmin(account, DesTool.encode(password));
				if(admin != null) {
					session.setAttribute("CurrentUser", admin);
					admin.setId(admin.getId());
					admin.setLandingTime(StringUtil.getStrDateTime());
					adminService.editAdminLandingTime(admin);
					errInfo = "success";
				} else {
					errInfo = "usererror";		
				}
			}
			else {
				errInfo = "codeerror";				 	//验证码输入有误
		}
		Gson gson=new Gson();
		str = gson.toJson(errInfo);
		//关于response的必要设置
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		response.setHeader("pragma","no-cache");
		response.setHeader("cache-control","no-cache");
		//传输JSON
		PrintWriter out = response.getWriter();
		out.println(str);
		out.flush();
		
	}	
	
}


